<?php

namespace app\admin\controller;

use app\admin\controller\Base;
use app\common\model\FileLog;
use app\common\model\VisitShow;
use app\common\model\Material;
use app\common\model\MaterialBanner;
use app\common\model\MaterialType;
use think\facade\View;
use think\facade\Config;
use think\facade\Cache;


class Materials extends Base
{   
    
    // 素材分类列表
    public function materialTypeList()
    {
        if (request()->IsAjax()) {
            // 查询权限
            $MaterialType = new MaterialType();
            $sort = 'sort ' . Config::get('sysconfig')['sort'];
            $Materiallist = $MaterialType->getAllOrder([], '', $sort);
            return json(["code" => 0, "msg" => "", "count" => 20, "data" => $Materiallist]);
        } else {
            return View::fetch();
        }
    }
    // 素材分类删除
    public function delMaterialType()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();
            if (isset($post['id']) && $post['id']) {
                
                $Material = new Material();
                $list = $Material->where(['type_id'=>$post['id']])->find();
                if(!empty($list)){
                    return json(['code' => 0, 'msg' => '分类下面有素材，请先删除素材']);
                }

                
                $list = $Material->where(['type_child_id'=>$post['id']])->find();
                if(!empty($list)){
                    return json(['code' => 0, 'msg' => '分类下面有素材，请先删除素材']);
                }

                try {
                    MaterialType::destroy($post['id']);
                    // 删除子集
                    $MaterialType = new MaterialType();
                    $MaterialType->destroyMaterial($post['id']);
                    return json(['code' => 1, 'msg' => '删除成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '删除失败']);
                }
            } else {
                return json(['code' => 0, 'msg' => '缺少参数']);
            }
        }
    }
    // 素材分类修改
    public function editMaterialType()
    {
        // 查询顶级及其子类
        $get  = $this->request->get();
        if (isset($get['id']) && $get['id']) {
            $one =  MaterialType::find($get['id']);
            $MaterialType = new MaterialType;
            $MaterialList = $MaterialType->getMaterial(0);
            View::assign('one', $one);
            View::assign('MaterialList', $MaterialList);
            // var_dump($MaterialList);
            // die;
            return View::fetch();
        } else {
            return json(['code' => 0, 'msg' => '缺少参数']);
        }
    }

    // 素材分类保存
    public function saveMaterialType()
    {
        if (request()->IsAjax()) {
            $post = $this->request->post();
            $addMaterial = $post;
         
            // 判断修改
            if (isset($addMaterial['id']) && $addMaterial['id']) {
                try {
                    MaterialType::update($addMaterial);
                    return json(['code' => 1, 'msg' => '更新成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            } else {
                // 新增
                try {
                    MaterialType::create($addMaterial);
                    return json(['code' => 1, 'msg' => '新增成功']);
                } catch (\Exception $e) {
                    $msg = $e->getMessage();
                    return json(['code' => 0, 'msg' => '新增失败']);
                }
            }
        } else {
            // 查询顶级及其子类
            $Material = new MaterialType;
            $MaterialList = $Material->getMaterial(0);
            View::assign('MaterialList', $MaterialList);
            return View::fetch();
        }
    }

    // 素材列表
    public function materialList()
    {
        // 存储空间对应的域名
        $domain = config('sysconfig.qiniu_domain');
        if (request()->IsAjax()) {
            $get = $this->request->get();
            $limit = isset($get['limit']) && $get['limit'] ? $get['limit'] : 10;
            $page = isset($get['page']) && $get['page'] ? $get['page'] : 1;
            $Material = new Material();
            $sort = 'id desc';
            $materialList = $Material->getPagelist([], '', $sort, $limit, $page);

            $MaterialType = new MaterialType();
            foreach($materialList as $key=>$val){
                $val->logo = $domain."/".$val['logo'];
                $val->type_name = $MaterialType->where(['id'=>$val['type_id']])->value("name");
                $val->type_child_name = $MaterialType->where(['id'=>$val['type_child_id']])->value("name");

                if($val['status'] == 1){
                    $val->status = "上架";
                }else{
                    $val->status = "下架";
                }
                
            }

            $count = $Material->countRows([]);
            return json(["code" => 0, "msg" => '成功', "count" => $count, "data" => $materialList]);
        } else {
            $MaterialType = new MaterialType();
            $typeList = $MaterialType->where("parent_id",0)->order('sort asc')->order('id',"asc")->field("id,name,sort,parent_id")->select();
            View::assign('typeList', $typeList);
            return View::fetch();
        }
    }

    // 素材分类列表
    public function materialTypeListApi()
    {
        $post = $this->request->post();

        if(empty($post['type_id'])){
            return json(['code' => 0, 'msg' => '请上传类型id']);
        }
        $MaterialType = new MaterialType();
        $typeList = $MaterialType->where("parent_id",$post['type_id'])->order('sort asc')->order('id',"asc")->field("id,name,sort,parent_id")->select();

        return json(['code' => 1, 'msg' => '新增成功',"list"=>$typeList]);
        
    }

}
